<?php

/***
 * Custom controleur pour gérer le contenu danses
 */
class pages_controller extends Component {

    private $_classUse;
    private $_contentManager;

    /**
     * Chargement des classes nécessaire
     */
    public function __construct(){
        $this->_classUse = Base::Load(CLASS_STATIC);
        $this->_contentManager = Base::Load(CLASS_CONTENT_MANAGER);
    }

    /***
     * Routage des actions
     * @return void
     */
    public function defaut(){
        if(isset($_GET['param'][0])) {
            switch($_GET['param'][0]){
                case 'edit':
                    $this->edit($_GET['param'][1]);
                break;
                case 'delete':
                    $this->delete($_GET['param'][1]);
                break;
                default:
                    $this->create();
                break;

            }
        }else {
            $this->getAll();
        }
    }

    /***
     * Listing des éléments de la catégorie
     * @return void
     */
    private function getAll(){
        $data = $this->_classUse->getAll();
        $struct = $this->_classUse->getStruct();

        $StructIndex = $structIndexName = array();
        foreach($struct->types->type as $t){
            if(isset($t->index)) {
                $StructIndex[] = $t->id;
                $structIndexName[] = $t->name;
            }
        }

        // Dernière entré en 1er
        $data = array_reverse($data);

        $this->_view->assign('structName', $structIndexName);
        $this->_view->assign('structIndex', $StructIndex);
        $this->_view->assign('data', $data);
        $this->_view->addBlock('content', 'list.tpl');
    }


    /**
     * Efface un contenu en fonction de son ID
     * @param  $id string   Id du contenu
     * @return void
     */
     private function delete($id){
        if($this->_contentManager->remove($id))
            header('location: ../');
        exit();
    }

    /**
     * Edite un contenu en fonction de son ID
     * @param  $id string   Id du contenu
     * @return void
     */
    private function edit($id){
        $this->addBreadCrumb('Edition');
        $param = array(
            'title' => "Édition d'une page statique"
        );
        $this->_view->assign('formParam', $param);
        $this->_classUse->editForm('content', $id, 'save');
    }

    /***
     * Formulaire de création d'un contenu
     * @return void
     */
    private function create(){
        $this->addBreadCrumb('Nouveau');
        $param = array(
            'title' => 'Nouvelle page statique'
        );
        $this->_view->assign('formParam', $param);
        $this->_classUse->addForm('content', 'save');
    }

    /**
     * Sauvegarde le contenu d'un formulaire de Content Manager
     * @param  $data array  Contenu de formulaire
     * @return void
     */
    public function POST_save($data){
        if(empty($data['id'])){ // new
            if($this->_contentManager->save($data))
                header('location: '.$_SERVER['REDIRECT_URL'].'../');
        }
        else {// update
            if($this->_contentManager->save($data, $data['id']))
                header('location: '.$_SERVER['REDIRECT_URL']);
        }
        exit();
    }
}